package com.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.user.entity.Product;
import com.user.util.BaseDao;

public class ProductDaoImpl {
	/*
	 * 添加用户方法
	 */
	public boolean addProduct(Product product){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement ps=con.prepareStatement("insert into product(id,name,description,listimg,price,discountprice) values(?,?,?,?,?,?)");
			ps.setInt(1, product.getId());
			ps.setString(2, product.getName());
			ps.setString(3, product.getDescription());
			ps.setString(4, product.getListimg());
			ps.setInt(5, product.getPrice());
			ps.setInt(6, product.getDiscountprice());
			int rs = ps.executeUpdate();
			if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	/*
	 * 验证该用户是否存在<根据username>
	 */
	public boolean isExist(String productname){
		return findProductByProductname(productname)==null?false:true;
	}
	/*
	 * 根据username查询相应用户
	 */
	public Product findProductByProductname(String productname){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement ps=con.prepareStatement("select * from pruduct where name=?");
			ps.setString(1, productname);
			ResultSet rs=ps.executeQuery();
			Product user=null;
			while(rs.next()){
				user=new Product();
				user.setId(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setDescription(rs.getString(3));
				user.setListimg(rs.getString(4));
				user.setPrice(rs.getInt(5));
				user.setDiscountprice(rs.getInt(6));
			}
			return user;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/*
	 * 查询用户列表
	 */
	public List<Product> findAll(){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement ps=con.prepareStatement("select * from product");
			ResultSet rs=ps.executeQuery();
			List<Product> list=new ArrayList<Product>();
			Product product=null;
			while(rs.next()){
				product=new Product();
				product.setId(rs.getInt(1));
				product.setName(rs.getString(2));
				product.setDescription(rs.getString(3));
				product.setListimg(rs.getString(4));
				product.setPrice(rs.getInt(5));
				product.setDiscountprice(rs.getInt(6));
				list.add(product);
			}
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	

}
